package com.xiaoyu.base.entity;

import androidx.room.Entity;
import androidx.room.PrimaryKey;
import androidx.room.TypeConverters;
import androidx.annotation.NonNull;
import android.text.TextUtils;

import com.xiaoyu.base.converter.StringListConverter;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;

import in.srain.cube.request.JsonData;

@Entity(tableName = "province_city_table")
public class ProvinceCityEntity {

    @PrimaryKey
    @NonNull
    private String province;
    @TypeConverters({StringListConverter.class})
    private List<String> cityList;

    public ProvinceCityEntity() {
        province = "";
        cityList = new ArrayList<>();
    }

    public ProvinceCityEntity(JsonData jsonData) {
        province = jsonData.optString("province");
        cityList = new LinkedList<>();

        List<JsonData> cityDataList = jsonData.optJson("cities").toList();
        for (JsonData cityRawData : cityDataList) {
            String cityName = cityRawData.optString("city");
            cityList.add(cityName);
        }
    }

    @NonNull
    public String getProvince() {
        return province;
    }

    public void setProvince(@NonNull String province) {
        this.province = province;
    }

    public List<String> getCityList() {
        return cityList;
    }

    public void setCityList(List<String> cityList) {
        this.cityList = cityList;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o)
            return true;
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        ProvinceCityEntity otherEntity = (ProvinceCityEntity) o;
        if (!TextUtils.equals(province, otherEntity.province)) {
            return false;
        } else {
            if (cityList == null || otherEntity.cityList == null) {
                return false;
            } else if (cityList.size() != otherEntity.cityList.size()) {
                return false;
            } else {
                for (int i = 0; i < cityList.size(); i++) {
                    if (!TextUtils.equals(cityList.get(i), otherEntity.cityList.get(i))) {
                        return false;
                    }
                }
            }
        }
        return true;
    }

    @Override
    public int hashCode() {
        return Objects.hash(province);
    }

    @Override
    public String toString() {
        return "ProvinceCityEntity{" +
                "province='" + province + '\'' +
                ", cityList=" + cityList +
                '}';
    }
}
